package com.example.playnote

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.LayoutRes
import com.example.playnote.bean.ToastFlag
import com.just.agentweb.AgentWeb
import es.dmoral.toasty.Toasty

/**
 * getAgentWeb
 */
fun String.getAgentWeb(
    activity: Activity, webContent: ViewGroup,
    layoutParams: ViewGroup.LayoutParams,
) = AgentWeb.with(activity)//传入Activity or Fragment
    .setAgentWebParent(webContent, layoutParams)//传入AgentWeb 的父控件
    .useDefaultIndicator()// 使用默认进度条
    .createAgentWeb()//
    .ready()
    .go(this)!!

/**
 * toast
 */
fun Context.toasty(
    flag: ToastFlag, content: String
) {
    val toastBuilder = when (flag) {
        ToastFlag.SUCCESS -> Toasty.success(this, content)
        ToastFlag.ERROR -> Toasty.error(this, content)
        ToastFlag.NORMAL -> Toasty.normal(this, content)
    }
    toastBuilder.duration = Toast.LENGTH_SHORT
    toastBuilder.show()
}

/**
 * get random color
 * @return 16777215 is FFFFFF, 0 is 000000
 */
fun getRandomColor(): String = "#${Integer.toHexString((Math.random() * 16777215).toInt())}"

/**
 * LayoutInflater.from(this).inflate
 * @param resource layoutId
 * @return View
 */
fun Context.inflater(@LayoutRes resource: Int): View =
    LayoutInflater.from(this).inflate(resource, null)

